var cfg = require('../../utils/cfg');
var sendRequest = require('../../utils/requestUtils');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    image1: '../../images/upload.png',
    image2: '../../images/upload.png',
    image3: '../../images/upload.png',
    image1Name : '',
    image2Name: '',
    image3Name: '',
    junkNav : [],
    junkNav2 : [],
    navIndex : -1,
    navIndex2 : -1,
    hidenNav2 : true,
    junkNavValue : '请选择',
    junkNavValue2 : '',
    phone : '',
    address : ''

  },

  phoneHandler : function(event) {
    this.data.phone = event.detail.detail.value;
    console.log(this.data.remark);
  },
  addressHandler : function(event) {
    this.data.address = event.detail.detail.value;
    console.log(this.data.remark);
  },

  handleClick : function () {
    let that = this;
    let countImg = 0;
    if (that.data.image1 != '../../images/upload.png') {
      countImg ++;
    }
    if (that.data.image2 != '../../images/upload.png') {
      countImg ++;
    }
    if (that.data.image3 != '../../images/upload.png') {
      countImg ++;
    }

    if (that.data.phone == '') {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (that.data.address == '') {
      wx.showToast({
        title: '请输入地址',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    if (countImg == 0) {
      wx.showToast({
        title: '请上传物品图片',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    if (that.data.navIndex == -1 || that.data.navIndex2 == -1) {
      wx.showToast({
        title: '请选择回收分类',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    let navFirstId = that.data.junkNav[that.data.navIndex].id;
    let navSecondId = that.data.junkNav[that.data.navIndex].childs[that.data.navIndex2].id;

    wx.showModal({
      title: '提示',
      content: '确定要下单吗？',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')

          sendRequest.sendReq(cfg.djurl.junkOrder, {
            picName1 : that.data.image1Name,
            picName2 : that.data.image2Name,
            picName3 : that.data.image3Name,
            phone : that.data.phone,
            address : that.data.address,
            navFirst : navFirstId,
            navSecond : navSecondId,
            openid : app.globalData.openId
          }, function (result) {
              console.log(result.data);

              wx.redirectTo({
                url: '/pages/order/order'
              })

          });
        }
      }
    })


  },

  bindPickerChange: function(e) {
    let that = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    that.setData({
      navIndex: e.detail.value,
      navIndex2 : 0,
      junkNavValue:that.data.junkNav[e.detail.value].navname,
      hidenNav2 : false,
      junkNavValue2 : that.data.junkNav[e.detail.value].childs[0].navname,
      junkNav2 : that.data.junkNav[e.detail.value].childs
    })
  },

  bindPickerChange2: function(e) {
    let that = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    that.setData({
      navIndex2: e.detail.value,
      junkNavValue2:that.data.junkNav[that.data.navIndex].childs[e.detail.value].navname
    })
  },
  
  loadNav : function () {
    let that = this;
    sendRequest.sendReq(cfg.djurl.loadNav, {
    }, function (result) {
        console.log(result.data);
        that.setData({
          junkNav: result.data
        });
    });
  },

  bindMultiPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    })
  },

  choseImg : function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        console.log('tempFilePaths->' + tempFilePaths);

        wx.uploadFile({
          url: cfg.host + cfg.djurl.uploadPic,
          filePath: tempFilePaths[0],
          name: 'uploadFile',
          formData: {
            
          },
          success: function (res) {
            var data = JSON.parse(res.data)　　//坑2：与wx.request不同，wx.uploadFile返回的是[字符串]，需要自己转为JSON格式

            if (data.code != 1000) {
              return;
            }

            console.log('上传成功 data=' + data)

            that.setData({
              image1: data.data.picUrl,
              image1Name: data.data.picName,
            });
          }
        })

      }
    })
  },

  choseImg2: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        console.log('tempFilePaths->' + tempFilePaths);

        wx.uploadFile({
          url: cfg.host + cfg.djurl.uploadPic,
          filePath: tempFilePaths[0],
          name: 'uploadFile',
          formData: {

          },
          success: function (res) {
            var data = JSON.parse(res.data)　　//坑2：与wx.request不同，wx.uploadFile返回的是[字符串]，需要自己转为JSON格式

            if (data.code != 1000) {
              return;
            }

            console.log('上传成功 data=' + data)

            that.setData({
              image2: data.data.picUrl,
              image2Name: data.data.picName,
            });
          }
        })

      }
    })
  },

  choseImg3: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        console.log('tempFilePaths->' + tempFilePaths);

        wx.uploadFile({
          url: cfg.host + cfg.djurl.uploadPic,
          filePath: tempFilePaths[0],
          name: 'uploadFile',
          formData: {

          },
          success: function (res) {
            var data = JSON.parse(res.data)　　//坑2：与wx.request不同，wx.uploadFile返回的是[字符串]，需要自己转为JSON格式

            if (data.code != 1000) {
              return;
            }

            console.log('上传成功 data=' + data)

            that.setData({
              image3: data.data.picUrl,
              image3Name: data.data.picName,
            });
          }
        })

      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadNav();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})